import { Component,OnInit,Input } from '@angular/core';
import { Passenger } from './passenger';
import { Router } from '@angular/router';

import { PassengerService } from './passenger.service';
import { Order } from '../order/order';
import { OrderService } from '../order/order.service';

@Component({
  selector: 'my-passenger',
  templateUrl: './passenger.component.html'
})

export class PassengerComponent implements OnInit {
  ngOnInit() {
    this.getPassengers();
  }
  // @Input()
  // passengerSearch: Passenger;

  passenger_id: string;
  passenger_name: string;
  
  selectedOrders: Order[];
  passengers: Passenger[]; 
  selectedPassenger: Passenger;

  constructor(
    private router: Router,
    private passengerService: PassengerService,
    private orderService: OrderService) {
   
  }
  getOrders(){
    this.orderService.getOrdersBypsid(this.selectedPassenger.passenger_id).then(orders => this.selectedOrders = orders);
  }

  onSelect(passenger: Passenger) { 
    this.selectedPassenger = passenger; 
    this.getOrders();
  }

  // onNew() { this.selectedPassenger = new Passenger(); }


  onSearch(){
    this.getPassengersBy();
  }

  getPassengers() {
    this.passengerService.getPassengers().then(passengers => this.passengers = passengers);
  }

  getPassengersBy(){
    this.passengerService.searchPassengers(this.passenger_id,this.passenger_name).then(passengers => this.passengers = passengers);
  }

}
